import { Body, Controller, Get, Post, Res, UseInterceptors, Request, HttpException, HttpStatus } from '@nestjs/common';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { apibefore } from 'src/router';
import { Reward } from './reward.entity';
import { RewardService } from './reward.service';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
import { User } from '../user/user.entity';
import { UserService } from '../user/user.service';

@Controller(apibefore('/reward'))
export class RewardController {
  constructor(private readonly service: RewardService,
    private readonly userServices: UserService,
    ) {}

  @Post('/create')
  @UseInterceptors(ValidTokenInterceptor)
  @UseInterceptors(ValidRSAInterceptor)
  create(@Body() data: Reward, @Request() req){
    let wx_user = req.headers.wx_user
    return this.service.create(data, wx_user)
  }

  @Get('/find_by_wx_user')
  @UseInterceptors(ValidTokenInterceptor)
  @UseInterceptors(ValidRSAInterceptor)
  find_by_wx_user(@Request() req){
    return this.service.find_by_wx_user(req.headers.wx_user)
  }

  @Post('/bigReward')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async bigReard(@Body() data, @Request() req){
    let video_id = data.video_id
    let token: number = req.headers.token
    //查询用户的wx_user
    let findUser:User = await this.userServices.findOne(token)
    if(!findUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let err = await this.service.bigReward(video_id, findUser.wx_user)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }

}
